﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.editorControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.SessionState;
using System.Reflection;
using umbraco.NodeFactory;
using ExtendedWorkFlow.Helpers;
using ExtendedWorkFlow.Commands;
using umbraco.uicontrols;

namespace ExtendedWorkFlow.Pages
{

    public class WorkBox : umbraco.BasePages.UmbracoEnsuredPage
    {

        List<int> _WorkFlowIds = new List<int>();

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            using (IRecordsReader objReader = WorkFlowHelper.GetUserWorkBox(umbraco.helper.GetCurrentUmbracoUser()))
            {
                while (objReader.Read())
                {
                    int intId = objReader.GetInt("id");

                    if (!this._WorkFlowIds.Contains(intId))
                        this.Form.Controls.Add(CreateWorkboxEntry(objReader));

                    this._WorkFlowIds.Add(intId);
                }
            }
        }

        public Control CreateWorkboxEntry(IRecordsReader Reader)
        {
            Node objState = new Node(Reader.GetInt("stateid"));

            int intId = Reader.GetInt("id");
            int intDocumentId = Reader.GetInt("documentid");

            Table tblLayout = new Table();
            TableRow rowLayout = new TableRow();
            TableCell celName = new TableCell();
            TableCell celCommands = new TableCell();

            tblLayout.Width = new Unit("100%");

            celName.Width = new Unit("100px");
            celCommands.Style.Add("text-align", "right");

            Pane ppWorkFlow = new Pane();
            ppWorkFlow.Text = objState.Name;

            celName.Text = "Doucment: " + Reader.GetString("text");

            // Add actions
            foreach (Node objAction in objState.Children)
            {
                if (string.Compare(objAction.NodeTypeAlias, "action", true) == 0)
                {
                    LinkButton lnkCommand = new LinkButton();
                    lnkCommand.Text = objAction.Name;
                    lnkCommand.CommandName = intId.ToString();
                    lnkCommand.CommandArgument = objAction.Id.ToString();

                    lnkCommand.Click += new EventHandler(lnkCommand_Click);

                    if (objAction.Properties["description"] != null)
                        lnkCommand.ToolTip = objAction.Properties["description"].Value;

                    // Add Action link
                    celCommands.Controls.Add(lnkCommand);
                }
            }

            rowLayout.Cells.Add(celName);
            rowLayout.Cells.Add(celCommands);
            tblLayout.Rows.Add(rowLayout);

            ppWorkFlow.Controls.Add(tblLayout);

            return ppWorkFlow;
        }

        void lnkCommand_Click(object sender, EventArgs e)
        {
            LinkButton lnkButton = sender as LinkButton;

            if (lnkButton != null)
            {
                // Get Ids
                int inttWorkFlowId = Convert.ToInt32(lnkButton.CommandName.ToString());
                int intActionId = Convert.ToInt32(lnkButton.CommandArgument.ToString());

                // Action node
                WorkFlow objWorkFlow = new WorkFlow(inttWorkFlowId);
                Node objAction = new Node(intActionId);

                if (objWorkFlow.Id != 0 &&
                    objAction.Id != 0)
                {
                    // Check if just move next
                    if (objAction.Properties["state"] == null)
                        objWorkFlow.NextState();
                }
            }
        }


    }

}
